The invention is generally related to computers and computer software. More specifically, the invention is generally related to compilation and interpretation of string concatenations and other operations that utilize temporary data storage.
Managing memory is critically important to the performance and reliability of a data processing system such as a computer. Specifically, data used by a computer program is typically stored in a computer within a memory that has a limited address space. In many computers, data is stored in the form of xe2x80x9cobjectsxe2x80x9d that are allocated space in a portion of the memory referred to as an xe2x80x9cobject heapxe2x80x9d.
In many computer environments, objects are created, or xe2x80x9callocatedxe2x80x9d, dynamically (i.e., on an as-needed basis) during the execution of a computer program. In addition, given that the amount of memory available in a computer is limited, some mechanism for removing, or xe2x80x9cdeallocatingxe2x80x9d, unused objects is also provided, typically either through the use of specific program instructions or through an automated process known as garbage collection. One well known computer programming environment that utilizes dynamic memory allocation, for example, is the Java programming environment developed by Sun Microsystems.
One primary benefit of dynamic memory allocation is that the overall amount of memory required to run a computer program is typically reduced, often reducing the memory requirements of a computer upon which the program is executed, or permitting more computer programs to share a given memory space in the computer. Computer performance may also benefit due to the reduced memory requirements, since less swapping of data into and out of memory may be required. Another benefit is that overall reliability of the computer is often improved since memory usage is typically better managed.
The primary drawback to dynamic memory management, however, is the additional xe2x80x9coverheadxe2x80x9d that is associated with both allocating and deallocating objects. Allocating and deallocating an object each require the computer to perform certain operations that are outside of the actual productive operations that are being performed during execution of a computer program. These additional operations delay the performance of the productive operations, which typically slows the overall operation of the computer. Therefore, for performance concerns, it is highly desirable to minimize whenever possible the number of object allocation and deallocation operations performed by a computer program.
The overhead associated with object allocation and deallocation is of particular concern with respect to computer operations that rely on xe2x80x9ctemporaryxe2x80x9d data storage. In particular, with objects that are used repeatedly over a significant period of time, the overhead associated with allocating and deallocating those objects becomes relatively insignificant compared to the productive operations that are performed with those objects. In contrast, whenever objects are only used for a brief period of time, the overhead associated with allocating and then deallocating the objects becomes more significant relative to the productive operations performed with those objects. Furthermore, although the overhead associated with each allocation and deallocation may be relatively minor, when the effects of a large number of operations that rely on temporary objects are considered, the collective impact on overall computer performance can be substantial.
A skilled computer programmer can in some instances reduce the number of object allocation and deallocation operations in a computer program during development of the computer program. In other instances, however, the programmer may not be able to control when some objects are allocated and/or deallocated. For example, a programmer writing a computer program in a source code, or human-readable format, may need to use certain instructions that, when translated into a machine-readable or intermediate format, inherently result in the creation of temporary objects due to the design of the particular translation program (e.g., a compiler or interpreter) used to translate the computer program to the machine-readable or intermediate format.
One such example is a string concatenation operation in the Java programming environment. A string concatenation operation, which is designated by the xe2x80x9c+xe2x80x9d operator in Java, creates a new string from the two objects identified as arguments to the operation. For example, the concatenation of a first string such as xe2x80x9cstartxe2x80x9d with a second string such as xe2x80x9c1exe2x80x9d would result in the creation of a new string xe2x80x9cstartlexe2x80x9d.
Compilation of a string concatenation statement in a Java source code program by a Java compiler results in the generation of program code that utilizes a temporary mutable string object, known in Java as a xe2x80x9cStringBufferxe2x80x9d object, in performing the string concatenation operation. As an example, Table I below illustrates an exemplary xe2x80x9cmyExamplexe2x80x9d Java class that includes an xe2x80x9cexampleConcatxe2x80x9d procedure that receives two arguments xe2x80x9cs1 xe2x80x9d and xe2x80x9cs2xe2x80x9d and returns a xe2x80x9cresultxe2x80x9d object that is the concatenation of the data in the xe2x80x9cs1xe2x80x9d and xe2x80x9cs2xe2x80x9d arguments:
Compilation of the statement xe2x80x9cString result=s1+s2xe2x80x9d using a conventional Java compiler results in the generation of program code that generally corresponds to the statement shown in Table II below:
The above statement can be parsed into the following operations:
1. String.valueOf(s1)xe2x80x94returns a string representation of xe2x80x9cs1xe2x80x9d
2. new StringBuffer( . . . )xe2x80x94creates a new xe2x80x9cStringBufferxe2x80x9d object containing the string representation of xe2x80x9cs1xe2x80x9d
3. .append(s2)xe2x80x94appends xe2x80x9cs2xe2x80x9d to the xe2x80x9cStringBufferxe2x80x9d object
4. .toString( )xe2x80x94creates a new xe2x80x9cStringxe2x80x9d object from the xe2x80x9cStringBufferxe2x80x9d object
Operation 2 above results in the allocation of two objects: a StringBuffer object and an underlying character array object used by the StringBuffer object. Operation 4 above creates one additional object: a String object that is returned as the result of the operation. Operations 1 and 3 typically do not result in the creation of any objects, although in some circumstances additional objects may need to be created handle format conversion, for example if either s1 or s2 was in a format other than a string (e.g., an integer, a boolean value, etc.), or when the existing character array is not large enough to hold all of the characters in s1 and s2. Therefore, a minimum of three objects are created for each string concatenation operation. Of these three objects, the StringBuffer object and its underlying character array object are not used after completion of the string concatenation operation. In most circumstances, both such objects are eventually deallocated by the Java garbage collector some time after completion of the string concatenation operation.
String concatenation operations are used extensively in a number of Java applications such as manipulating results from database files, and generating dynamic documents (e.g., hypertext markup language (HTML) documents and the like), among others. As such, it is possible in many applications for string concatenations to result in the creation of a relatively large number of temporary objects, which can have a significant negative impact on overall system performance.
Therefore, a substantial need exists in the art for a manner of improving the performance of a computer in performing operations that rely on temporary data storage, and in particular, for a manner of improving the performance of a computer in performing string concatenations and other like operations.
The invention addresses these and other problems associated with the prior art by providing an apparatus, program product, and method of translating a first computer program into a second computer program in which program code is generated to utilize a reusable temporary object in the performance of multiple operations that require the use of temporary storage. As such, the reusable temporary object need only be allocated once, in contrast with conventional implementations where multiple temporary objects would otherwise need to be allocated in the performance of such operations. Consequently, the additional overhead associated with allocating memory for additional objects is eliminated, which improves overall performance while handling such multiple operations. Furthermore, in implementations where objects need to be collected (either explicitly or through automatic collection), the additional overhead associated with collecting the additional objects when they are no longer in use is also avoided, providing further improvements in performance over conventional implementations.
In one specific, but by no means exclusive, implementation, a reusable temporary object is provided in the form of a mutable string object, which is utilized in the performance of string concatenation operations in the Java programming environment. Consequently, rather than creating a new mutable string object (as well as an underlying character array object) for each string concatenation operation, an existing mutable string object, allocated at the initialization of a program (or a thread thereof), is used as the temporary storage for each operation. The total number of objects created as a result of multiple string concatenation operations is therefore reduced, easing allocation and collection overhead, and accordingly improving overall system performance.
These and other advantages and features, which characterize the invention, are set forth in the claims annexed hereto and forming a further part hereof. However, for a better understanding of the invention, and of the advantages and objectives attained through its use, reference should be made to the Drawings, and to the accompanying descriptive matter, in which there is described exemplary embodiments of the invention.